<?php

namespace app\api\controller;




use app\api\model\hotel\HotelRoom as HotelRoomModel;
use app\api\model\hotel\HotelRoomConfig;
use app\common\controller\Api;

/**
 * 酒店房间接口
 */
class Hotelroom extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    //房间列表
    public function lists()
    {
        $room_model = new HotelRoomModel;
        $hotel_id = input('hotel_id',0);

        if (empty($hotel_id))$this->error('缺少参数酒店id');

        $list = HotelRoomModel::where(['hotel_id'=>$hotel_id,'status'=>'normal'])
            ->field('room_id,hotel_id,room_name,image,label,price')
            ->order('weigh desc')->select();
        foreach ($list as $k=>$v){
            if ($this->auth->id){
                $member_price = $room_model->getRoomPrice($v['room_id'],$this->auth->id);

                if ($member_price['member_price']){
                    $list[$k]['member_price'] = $member_price['member_price'];
                }
            }
        }

        $this->success('',$list);
    }

    //房间详情
    public function info()
    {
        $room_id = input('room_id',0);

        if (empty($room_id))$this->error('缺少参数房间id');

        $info =  HotelRoomModel::where(['room_id'=>$room_id,'status'=>'normal'])
            ->field('room_id,hotel_id,room_name,image,label,price,basic,bathroom')
            ->find();
        if (empty($info))$this->error('房间已下架');

        $info['basic_list'] = HotelRoomConfig::where('id','in',$info['basic'])->column('name');
        $info['bathroom_list'] = HotelRoomConfig::where('id','in',$info['bathroom'])->column('name');
        if ($this->auth->id){
            $room_model = new HotelRoomModel;
            $member_price = $room_model->getRoomPrice($room_id,$this->auth->id);

            if ($member_price['member_price']){
                $info['member_price'] = $member_price['member_price'];
            }
        }
        $this->success('',$info);
    }

    public function canCheckin()
    {
        $room_id = input('room_id',0);
        $checkin_date = input('checkin_date',0);
        $leave_date = input('leave_date',0);
        $room_info = \app\api\model\hotel\HotelRoom::get($room_id);
        if ($checkin_date&&$leave_date){

            $order_list = collection(\app\api\model\hotel\HotelOrder::where(['room_id'=>$room_id])
                ->where('order_status','in','1,10')
                ->field('checkin_date,leave_date')->select())->toArray();

            $user_dates = $this->generate_dates($checkin_date, $leave_date);
            array_pop($user_dates);

            $conflict = $this->check_conflict($order_list, $user_dates);

            $this->success('',$room_info['stock']-$conflict);
            /*if ($conflict) {
                $this->success('',0);
            } else {
                $this->success('',1);
            }*/
        }
        $this->success('',$room_info['stock']);
    }

    // 生成用户选择的日期范围
    public function generate_dates($start, $end) {
        $dates = array();
        $current = new \DateTime($start);
        while ($current <= new \DateTime($end)) {
            $dates[] = $current->format('Y-m-d');
            $current->modify('+1 day');
        }
        return $dates;
    }

    // 检查与已订房的时间是否有冲突
    public function check_conflict($booked_dates, $user_dates) {

        $count = 0;
        foreach ($booked_dates as $booking) {
            $booking_dates = $this->generate_dates($booking['checkin_date'], $booking['leave_date']);
            array_pop($booking_dates);

            // 检查是否有重叠的日期
            $overlap = array_intersect($user_dates, $booking_dates);
            if (!empty($overlap)) {
                $count++;
                //return true;
            }
        }

        return $count;
    }
}